//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;
using System.Text;

namespace SoapTrace.Tests.SoapPacketMonitoring
{
	/// <summary>
	/// Tests that the <see cref="SoapPacketMonitorBase"/> class's
	/// handling of a soap request and response.
	/// </summary>
	[ TestFixture ]
	public class SoapRequestAndResponseTestFixture : SoapPacketMonitorBase
	{
		SoapRequest Request;
		SoapResponse Response;
		
		/// <summary>
		/// Initialises the test fixture.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			Request = null;
			Response = null;
			
			base.SetFilter( "209.171.52.99", 80 );
			
			StringReader Reader = new StringReader( GetPacketXml( ) );
			PacketLogEntryCollection LogEntries = PacketLogFile.Load( Reader );
			
			foreach( PacketLogEntry Entry in LogEntries )
			{
				base.ProcessPacket( Entry.Packet );
			}
		}
			
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void SoapRequestMessage( )
		{
			string ExpectedMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><GetMaxCommentListLength xmlns=\"http://codeproject.com/webservices/\" /></soap:Body>" +
				"</soap:Envelope>";
			
			Assert.AreEqual( ExpectedMessage, Request.Message, "Unexpected message received." );
		}
		
		/// <summary>
		/// Check the soap request header.
		/// </summary>
		[ Test ]
		public void SoapRequestHeader( )
		{
			string ExpectedHeader = "POST /webservices/latest.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"http://codeproject.com/webservices/GetMaxCommentListLength\"\r\n" +
				"Content-Length: 317\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: www.codeproject.com\r\n" +
				"\r\n";
			
			Assert.AreEqual( ExpectedHeader, Request.Header, "Unexpected request header received." );
		}
		
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void SoapResponseMessage( )
		{
			string ExpectedMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><GetMaxCommentListLengthResponse xmlns=\"http://codeproject.com/webservices/\">" + 
				"<GetMaxCommentListLengthResult>10</GetMaxCommentListLengthResult>" +
				"</GetMaxCommentListLengthResponse>" +
				"</soap:Body>" +
				"</soap:Envelope>";
			
			Assert.AreEqual( ExpectedMessage, Response.Message, "Unexpected message received." );
		}
		
		/// <summary>
		/// Check that the soap response header is correct.
		/// </summary>
		[ Test ]
		public void SoapResponseHeader( )
		{
			string ExpectedHeader = "HTTP/1.1 200 OK\r\n" +
				"Server: Microsoft-IIS/5.0\r\n" +
				"Date: Mon, 31 Jan 2005 18:43:11 GMT\r\n" +
				"X-Powered-By: ASP.NET\r\n" +
				"X-AspNet-Version: 1.1.4322\r\n" +
				"Cache-Control: no-cache\r\n" +
				"Pragma: no-cache\r\n" +
				"Expires: -1\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"Content-Length: 422\r\n" +
				"\r\n";
			
			Assert.AreEqual( ExpectedHeader, Response.Header, "Unexpected response header." );
		}
	
		/// <summary>
		/// Handles the soap request received event.
		/// </summary>
		protected override void OnSoapRequestReceived( object sender, SoapRequestReceivedEventArgs e )
		{
			Request = e.Request;
		}
		
		/// <summary>
		/// Handles the soap response received event.
		/// </summary>
		protected override void OnSoapResponseReceived( object sender, SoapResponseReceivedEventArgs e )
		{
			Response = e.Response;
		}		
		
		/// <summary>
		/// Gets the xml to be read in by the <see cref="PacketLogFile"/>
		/// class.
		/// </summary>
		private string GetPacketXml( )
		{
			return "<Packets>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.77\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 30 4B 02 40 00 80 06 E8 0B C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 7F 00 00 00 00 70 02 FF FF 4B 4F 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.83\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 30 1A EA 40 00 67 06 31 24 D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 EF C4 BD AA AB 80 70 12 FF FF EB DF 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.88\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 4B 03 40 00 80 06 E8 12 C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 80 6F C1 EF C5 50 10 FF FF 18 7C 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.93\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 01 7F 4B 04 40 00 80 06 E6 BA C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 80 6F C1 EF C5 50 18 FF FF F2 8F 00 00 50 4F 53 54 20 2F 77 65 62 73 65 72 76 69 63 65 73 2F 6C 61 74 65 73 74 2E 61 73 6D 78 20 48 54 54 50 2F 31 2E 31 0D 0A 55 73 65 72 2D 41 67 65 6E 74 3A 20 4D 6F 7A 69 6C 6C 61 2F 34 2E 30 20 28 63 6F 6D 70 61 74 69 62 6C 65 3B 20 4D 53 49 45 20 36 2E 30 3B 20 4D 53 20 57 65 62 20 53 65 72 76 69 63 65 73 20 43 6C 69 65 6E 74 20 50 72 6F 74 6F 63 6F 6C 20 31 2E 31 2E 34 33 32 32 2E 35 37 33 29 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 78 6D 6C 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 53 4F 41 50 41 63 74 69 6F 6E 3A 20 22 68 74 74 70 3A 2F 2F 63 6F 64 65 70 72 6F 6A 65 63 74 2E 63 6F 6D 2F 77 65 62 73 65 72 76 69 63 65 73 2F 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 22 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 33 31 37 0D 0A 45 78 70 65 63 74 3A 20 31 30 30 2D 63 6F 6E 74 69 6E 75 65 0D 0A 43 6F 6E 6E 65 63 74 69 6F 6E 3A 20 4B 65 65 70 2D 41 6C 69 76 65 0D 0A 48 6F 73 74 3A 20 77 77 77 2E 63 6F 64 65 70 72 6F 6A 65 63 74 2E 63 6F 6D 0D 0A 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:35.48\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 98 1A FE 40 00 67 06 30 A8 D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 EF C5 BD AA AC D7 50 18 FE A8 1F C6 00 00 48 54 54 50 2F 31 2E 31 20 31 30 30 20 43 6F 6E 74 69 6E 75 65 0D 0A 53 65 72 76 65 72 3A 20 4D 69 63 72 6F 73 6F 66 74 2D 49 49 53 2F 35 2E 30 0D 0A 44 61 74 65 3A 20 4D 6F 6E 2C 20 33 31 20 4A 61 6E 20 32 30 30 35 20 31 38 3A 34 33 3A 31 30 20 47 4D 54 0D 0A 58 2D 50 6F 77 65 72 65 64 2D 42 79 3A 20 41 53 50 2E 4E 45 54 0D 0A 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:35.53\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 01 65 4B 05 40 00 80 06 E6 D3 C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AC D7 6F C1 F0 35 50 18 FF 8F 48 2A 00 00 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 75 74 66 2D 38 22 3F 3E 3C 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 20 78 6D 6C 6E 73 3A 73 6F 61 70 3D 22 68 74 74 70 3A 2F 2F 73 63 68 65 6D 61 73 2E 78 6D 6C 73 6F 61 70 2E 6F 72 67 2F 73 6F 61 70 2F 65 6E 76 65 6C 6F 70 65 2F 22 20 78 6D 6C 6E 73 3A 78 73 69 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 2D 69 6E 73 74 61 6E 63 65 22 20 78 6D 6C 6E 73 3A 78 73 64 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 22 3E 3C 73 6F 61 70 3A 42 6F 64 79 3E 3C 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 20 78 6D 6C 6E 73 3D 22 68 74 74 70 3A 2F 2F 63 6F 64 65 70 72 6F 6A 65 63 74 2E 63 6F 6D 2F 77 65 62 73 65 72 76 69 63 65 73 2F 22 20 2F 3E 3C 2F 73 6F 61 70 3A 42 6F 64 79 3E 3C 2F 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 3E </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:35.59\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 1B 24 40 00 67 06 30 F2 D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 F0 35 BD AA AE 14 50 10 FD 6B 18 0C 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:35.64\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 01 22 1B 3B 40 00 67 06 2F E1 D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 F0 35 BD AA AE 14 50 18 FD 6B 48 73 00 00 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D 0A 53 65 72 76 65 72 3A 20 4D 69 63 72 6F 73 6F 66 74 2D 49 49 53 2F 35 2E 30 0D 0A 44 61 74 65 3A 20 4D 6F 6E 2C 20 33 31 20 4A 61 6E 20 32 30 30 35 20 31 38 3A 34 33 3A 31 31 20 47 4D 54 0D 0A 58 2D 50 6F 77 65 72 65 64 2D 42 79 3A 20 41 53 50 2E 4E 45 54 0D 0A 58 2D 41 73 70 4E 65 74 2D 56 65 72 73 69 6F 6E 3A 20 31 2E 31 2E 34 33 32 32 0D 0A 43 61 63 68 65 2D 43 6F 6E 74 72 6F 6C 3A 20 6E 6F 2D 63 61 63 68 65 0D 0A 50 72 61 67 6D 61 3A 20 6E 6F 2D 63 61 63 68 65 0D 0A 45 78 70 69 72 65 73 3A 20 2D 31 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 78 6D 6C 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 34 32 32 0D 0A 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:36.19\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 4B 06 40 00 80 06 E8 0F C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AE 14 6F C1 F1 2F 50 10 FE 95 15 E8 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:36.24\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 01 CE 1B 51 40 00 67 06 2F 1F D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 F1 2F BD AA AE 14 50 18 FD 6B 4F BE 00 00 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 75 74 66 2D 38 22 3F 3E 3C 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 20 78 6D 6C 6E 73 3A 73 6F 61 70 3D 22 68 74 74 70 3A 2F 2F 73 63 68 65 6D 61 73 2E 78 6D 6C 73 6F 61 70 2E 6F 72 67 2F 73 6F 61 70 2F 65 6E 76 65 6C 6F 70 65 2F 22 20 78 6D 6C 6E 73 3A 78 73 69 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 2D 69 6E 73 74 61 6E 63 65 22 20 78 6D 6C 6E 73 3A 78 73 64 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 22 3E 3C 73 6F 61 70 3A 42 6F 64 79 3E 3C 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 52 65 73 70 6F 6E 73 65 20 78 6D 6C 6E 73 3D 22 68 74 74 70 3A 2F 2F 63 6F 64 65 70 72 6F 6A 65 63 74 2E 63 6F 6D 2F 77 65 62 73 65 72 76 69 63 65 73 2F 22 3E 3C 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 52 65 73 75 6C 74 3E 31 30 3C 2F 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 52 65 73 75 6C 74 3E 3C 2F 47 65 74 4D 61 78 43 6F 6D 6D 65 6E 74 4C 69 73 74 4C 65 6E 67 74 68 52 65 73 70 6F 6E 73 65 3E 3C 2F 73 6F 61 70 3A 42 6F 64 79 3E 3C 2F 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 3E </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:36.29\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 4B 07 40 00 80 06 E8 0E C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AE 14 6F C1 F2 D5 50 10 FC EF 15 E8 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:41.80\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 4B 08 40 00 80 06 E8 0D C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AE 14 6F C1 F2 D5 50 14 00 00 12 D4 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"</Packets>";
		}
	}
}
